Often times software is created that needs to communicate with other software when certain events occur. For example, in a computer windowing system, when a user selects a window on the display, the window system needs to notify the software that is drawing information in the window that the window bas been selected. In prior systems, the software needing notification of certain events registers the events for which it wants to be notified with the software that raises the events. In come prior systems, as part of the registration mechanism, the software needing notification registers a notification function by which it can be notified. Then, when the software raises an event that was previously registered, the registered notification function is called. This is known in the prior art as a callback mechanism.
An overview of well-known object-oriented programming techniques is provided, since the present invention is described below using object-oriented concepts. Two common characteristics of object-oriented programming languages are support for data encapsulation and data type inheritance. Data encapsulation refers to the binding of functions and data. Inheritance refers to the ability to declare a data type in terms of other data types.
In the C++ language, object-oriented techniques are supported through the use of classes. A class is a user-defined type. A class declaration describes the data members and function members of the class. For example, the following declaration defines data members and a function member of a class named CIRCLE.
class CIRCLE{public; int x, y; int radius; void draw( );};Variables x and y specify the center location of a circle and variable radius specifies the radius of the circle. These variables are referred to as data members of the class CIRCLE. The function draw is a user-defined function that draws the circle of the specified radius at the specified location. The function draw is referred to as a function member of class CIRCLE. A function member is also referred to as a method of a class. The data members and function members of a class are bound together in that the function operates on an instance of the class. An instance of a class is also called an object of the class.
In the syntax of C++, the following statement declares the objects a and b to be of type class CIRCLE.
CIRCLE a, b;
This declaration causes the allocation of memory for the objects a and b. The following statements snip data to the data members of objects a and b.
a.x=2;
a.y=2;
a.radius=1:
b.x=4;
b.y=5;
b.radius=2;
The following statements are used to draw the circles defined by objects a and b.
a.draw( );
b.draw( );
A derived class is a class that inherits the characteristics—data members and function members—of its base classes. For example, the following derived class CIRCLE_FILL inherits the characteristics of the base class CIRCLE.
class CIRCLE_FILL: CIRCLE{public; int pattern; void fill( );};This declaration specifies that class CIRCLE_FILL includes all the data and function members that are in class CIRCLE in addition to those data and function members introduced in the declaration of class CIRCLE_FILL that is, data member pattern and function member fill. In this example, class CIRCLE_FILL has data members x, y, radius, and pattern and function members draw and fill. Class CIRCLE_FILL is said to “inherit” the characteristics of class CIRCLE. A class that inherits the characteristics of another class is a derived class (e.g., CIRCLE_FILL). A class that does not inherit the characteristics of another class is a primary (root) class (e.g., CIRCLE). A class whose characteristics are inherited by another class is a base class (e.g., CIRCLE is a base class of CIRCLE_FILL). A derived class may inherit the characteristics of seventh classes, that is, a derived class may have several base classes. This is referred to as multiple inheritance.
A derived class may specify that a base class is to be inherited virtually. Virtual inheritance of a base class means that only one instance of the virtual base class exists in the derived class. For example, the following is an example of a derived class with two nonvirtual base classes.
class CIRCLE_1: CIRCLE { . . . };
class CIRCLE_2: CIRCLE { . . . };
class PATTERN: CIRCLE_1, CIRCLE_2{ . . . };
In this declaration class PATTERN inherits class CIRCLE twice nonvirtually through classes CIRCLE_1 and CIRCLE_2. There are two instances of class CIRCLE in class PATTERN.
The following is as example of a derived class with two virtual base classes.
class CIRCLE_1: virtual CIRCLE { . . . };
class CIRCLE_2: virtual CIRCLE { . . . };
class PATTERN: CIRCLE_1, CIRCLE_2{ . . . };
The derived class PATTERN inherits class CIRCLE twice virtually through classes CIRCLE_1 and CIRCLE_2. Since the class CIRCLE is virtually inherited twice, there is only one object of class CIRCLE is the derived class PATTERN. One skilled in the art would appreciate virtual inheritance can be very useful when the class derivation is more complex.
A class may also specify whether its function members are virtual. Declaring that a function member is virtual means that the function can be overridden by a function of the same name and type in a derived class. In the following example, the function draw is declared to be virtual in classes CIRCLE and CIRCLE_FILL.
class CIRCLE{public; int x, y; int radius; virtual void draw( );};class CIRCLE_FILL: CIRCLE{public; int pattern; virtual void draw( );};If a virtual function is declared without providing in implementation, then it is referred to as a pure virtual function. A pure virtual function to a virtual function declared with the pure specifier, “=Ø”. If a class specifies a pure virtual function, then any desired class needs to specify an implementation for that function member before that function member may be invoiced.
In order to access objects, the C++ language provides a pointer data type. A pointer holds values that are addresses of objects in memory. Through a pointer, in object can be referenced. The following statement declares variable c_ptr to be a pointer on an object of type class CIRCLE and sets variable c_ptr to hold the address of object c.
CIRCLE *c_ptr,
c_ptr=&c;
Continuing with the example, the following statement declares object a to be of type class CIRCLE and object b to be of type class CIRCLE_FILL.
CIRCLE a:
CIRCLE_FILL b;
The following statement refers to the function draw as defined in class CIRCLE
a. draw( );
Whereas, the following statement refers to the function draw defined in class CIRCLE_FILL.
b.draw( );
Moreover, the following statements type cast object b to an object of type class CIRCLE and invoke the function draw that is defined in class CIRCLE_FILL.
CIRCLE *c_ptr;
c_ptr=&b;
c_ptr->draw( ); // CIRCLE_FILL::draw( )
Thus, the virtual function that is called is function CIRCLE_FILL::draw.
FIG. 1 is a block diagram illustrating typical data structures used to represent an object. An object is composed of instance data (data member) and member functions, which implement the behavior of the object. The data structures used to represent an object comprise instance data structure 101, virtual function table 102, and the function members 103, 104, 105. The instance data structure 101 contains a pointer to the virtual function table 102 and contains data members. The virtual function table 102 contains in entry for each virtual function member defined for the object. Each entry contains a reference to the code that implements the corresponding function member. The layout of this sample object confirms to the model defined in U.S. patent application Ser. No. 07/682,537, entitled “A Method for Implementing Virtual Functions and Virtual Bases in a Compiler for an Object Oriented Programming Language,” which is hereby incorporated by reference. In the following, an object will be described as an instance of a class as defined by the C++ programming language. One skilled in the art would appreciate that objects can be defined using other programming languages.
An advantage of using object-oriented techniques is that these techniques an be used to facilitate the sharing of objects. In particular, object-oriented techniques facilitate the creation of compound documents. A compound document is a document that contains objects generated by various computer programs. (Typically, only the data members of the object and the class type are stored in a compound document.) For example, a word processing document that contains a spreadsheet object generated by a spreadsheet program is a compound document. A word processing program allows a user to embed a spreadsheet object (e.g., a cell) within a word processing document. To allow this embedding, the word processing program is compiled using the class definition of the object to be embedded to access function members of the embedded object. Thus, the word processing program would need to be compiled using the class definition of each class of objects that can be embedded in a word processing document. To embed as object of a new class into a word processing document, the word processing program would need to be recompiled with the new class definition. Thus, only objects of classes selected by the developer of the word processing program can be embedded. Furthermore, new classes can only be supported with a now release of the word processing program.
To allow objects of an arbitrary class to be embedded into compound documents, interfaces are defined through which an object can be accessed without the need for the word processing program to have access to the class definitions at compile time. An abstract class is a class in which there is at least one virtual function member with no implementation (a pure virtual function member). An interface is an abstract class with no data members and whose virtual functions are all pure. Thus, an interface provides a protocol for two programs to communicate. Interfaces are typically used for derivation: a program implements classes that provide implementations for the interfaces the classes are derived from. Thereafter, objects are created as instances of these derived classes.
The following class definition is in example definition of in interface. In this example, for simplicity of explanation, rather than allowing any class of object to be embedded in its documents, a word processing program allows spreadsheet objects to be embedded. Any spreadsheet object that provides this interface can be embedded, regardless of how the object is implemented. Moreover, any spreadsheet object, whether implemented before or after the word processing program is compiled, can be embedded.
classISpreadSheet{virtual void File( ) = 0;virtual void Edit( ) = 0;virtual void Formula( ) = 0;virutal void Format( ) = 0;virtual void GetCell (string RC, cell *pCell) = 0;virtual void Data( ) = 0;}The developer of a spreadsheet program would need to provide an implementation of the interface to allow the spreadsheet objects to be embedded to a word processing document.
When the word processing program embeds a spreadsheet object, the program needs access to the code that implements the interface for the spreadsheet object. To access the class code, each implementation is given a unique class identifier. For example, code implementing a spreadsheet object developed by Microsoft Corporation may have a class identifier of “MSSpreadsheet,” while code implementing a spreadsheet object developed by another corporation may have a class identifier of “LTSSpreadsheet.” A persistent registry in each computer system is maintained that maps each class identifier to the code that implements the class. Typically, when a spreadsheet program is installed on a computer system, the persistent registry is updated to reflect the availability of that class of spreadsheet objects. So long as a spreadsheet developer implements each function member defined by the interface and the persistent registry is maintained, the word processing program can embed instances of the developer's spreadsheet objects into a wood processing document. The word processing program accesses the functional members of the embedded spreadsheet objects without regard to who has implemented them or how they have been implemented.
Various spreadsheet developers may wish, however, to implement only certain function members. For example, a spreadsheet developer may not want to implement database support, but may want to support all other function members. To allow a spreadsheet developer to support only some of the function members, while still allowing the objects to be embedded, multiple interfaces for spreadsheet objects are defined. For example, the, interfaces IDatabase and IBasic may be defined for a spreadsheet object as follows.
classIBasic{virtual void File( ) = 0;virtual void Edit( ) = 0;virtual void Formula( ) = 0;virtual void Format( ) = 0;virtual void GetCell (string RC, cell *pCell) = 0;}classIDatabase{virtual void Data( ) = 0;}Each spreadsheet developer would implement the IBasic interface and, optionally, the IDatabase interface.
At run time, the word processing program would need to determine whether a spreadsheet object to be embedded supports the IDatabase interface. To make this determination, another interface is defined (that every spreadsheet object implements) with a function member that indicates which interfaces are implemented for the object. This interface is named IUnknown (and referred to as the unknown interface or the object management interface) and is defined as follows.
class IUnknown{ virtual HRESULT QueryInterface (REFIID iid, void**ppv) = 0; virtual ULONG AddRef( ) = 0; virtual ULONG Release ( ) = 0;}The IUnknown interface defines the function member (method) QueryInterface. The method QueryInterface is passed in interface identifier (e.g., “IDatabase”) in parameter iid (of type REFIID) and returns a pointer to the implementation of the identified interface for the object for which the method is invoked is parameter ppv. If the object does not support the interface, then the method returns a false. The type HRESULT indicates a predefined status, and the type ULONG indicates an unsigned long integer.
Code TABLE 1HRESULT XX::QueryInterface(REFIID iid, void **ppv){ ret = TRUE; switch (iid) {  case IID_IBasic;   *ppv = *pIBasic;   break;  case IID_IDatabase;   *ppv = *pIDatabase;   break;  case IID_IUnknown;   *ppv = this;   break;  default;   ret = FALSE; } if (ret == TRUE) {AddRef( );); return ret;}
Code Table 1 contains pseudocode for C++ source code for a typical implementation of the method QueryInterface for class XX, which inherits the class IUnknown. If the spreadsheet object supports the IDatabase interface, then the method QueryInterface includes the appropriate case label within the twitch statement The variables plBasic and plDmabase point to a points to the virtual function tables of the IBasic end IDatabase interfaces, respectively. The method QueryInterface invokes to method AddRef (described below) to increment a reference count for the object of class XX when a pointer to an interface is returned.